<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Goods;

/**
 * GoodsSearch represents the model behind the search form about `common\models\Goods`.
 */
class GoodsSearch extends Goods
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['g_id', 'gf_id', 's_id'], 'integer'],
            [['g_name', 'g_intrlduce', 'g_img', 'img1', 'img2', 'img3', 'img4', 'g_data'], 'safe'],
            [['g_price', 'g_num', 'g_status', 'g_shenhe'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Goods::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'g_id' => $this->g_id,
            'gf_id' => $this->gf_id,
            's_id' => $this->s_id,
            'g_price' => $this->g_price,
            'g_num' => $this->g_num,
            'g_data' => $this->g_data,
            'g_status' => $this->g_status,
            'g_shenhe' => $this->g_shenhe,
        ]);

        $query->andFilterWhere(['like', 'g_name', $this->g_name])
            ->andFilterWhere(['like', 'g_intrlduce', $this->g_intrlduce])
            ->andFilterWhere(['like', 'g_img', $this->g_img])
            ->andFilterWhere(['like', 'img1', $this->img1])
            ->andFilterWhere(['like', 'img2', $this->img2])
            ->andFilterWhere(['like', 'img3', $this->img3])
            ->andFilterWhere(['like', 'img4', $this->img4]);

        return $dataProvider;
    }
}
